package com.hljx.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.hljx.entity.UndrugInfo;
import com.hljx.mapper.his.HisAdviceItemMapper;
import com.hljx.mapper.sys.UndrugInfoMapper;
import com.hljx.service.UndrugInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 非药品信息维护 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-12-13
 */
@Service
public class UndrugInfoServiceImpl extends ServiceImpl<UndrugInfoMapper, UndrugInfo> implements UndrugInfoService {

    @Autowired
    private HisAdviceItemMapper  hisAdviceItemMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUnDrugInfo() {
        baseMapper.delAll();
        List<UndrugInfo> undrugInfos = hisAdviceItemMapper.listAllUnDrugInfo();

        int total1 = undrugInfos.size();//为防止插入字符超过最大数,分批插入
        int fromIndex1 = 0;
        List<UndrugInfo> insertList1 = new ArrayList<>();
        for (int i = 0; i < undrugInfos.size(); i++) {
            UndrugInfo item = undrugInfos.get(i);
            fromIndex1++;
            insertList1.add(item);
            if (fromIndex1 % 2000 == 0 && fromIndex1 <= total1) {
                saveBatch(insertList1);
                insertList1 = new ArrayList<>();
            }
        }
        if (CollectionUtils.isNotEmpty(insertList1)) {
            saveBatch(insertList1);
        }

    }
}
